/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.wechat.entity.Witticism;
import com.boxin.ims.modules.wechat.dao.WitticismDao;

/**
 * 俏皮话Service
 * @author Jakemanse
 * @version 2013-11-14
 */
@Component
@Transactional(readOnly = true)
public class WitticismService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(WitticismService.class);
	
	@Autowired
	private WitticismDao witticismDao;
	
	public Witticism get(Long id) {
		return witticismDao.findOne(id);
	}
	
	
	
	public Page<Witticism> find(Page<Witticism> page, Witticism witticism) {
		DetachedCriteria dc = witticismDao.createDetachedCriteria();
		if (witticism.getUser()!=null && witticism.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", witticism.getUser().getId()));
		}
		
		if(StringUtils.isNotEmpty(witticism.getContent())){
			dc.add(Restrictions.like("content", "%"+witticism.getContent()+"%"));
		}
		
		if(StringUtils.isNotEmpty(witticism.getShareType())){
			dc.add(Restrictions.eq("shareType",  witticism.getContent()));
		}
		
		if(StringUtils.isNotEmpty(witticism.getType())){
			dc.add(Restrictions.eq("type",  witticism.getType()));
		}
		
		
		
		
		
		dc.addOrder(Order.desc("id"));
		return witticismDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(Witticism witticism) {
		witticism.setContent(StringEscapeUtils.unescapeHtml4(witticism.getContent()) );
		witticismDao.save(witticism);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		witticismDao.deleteById(id);
	}
	
}
